package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        //先判断是否存在该商品，如果存在，则数量+1，如果不存在，则新增一条记录
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);
        ShoppingCart aNew = shoppingCartMapper.shopByShop(shoppingCart);
        if (aNew==null){
            //判断是套餐还是菜品
            Dish dishById = dishMapper.getDishById(shoppingCart.getDishId());
            if (dishById!=null){
                //菜品
                shoppingCart.setDishFlavor(shoppingCart.getDishFlavor());
                shoppingCart.setImage(dishById.getImage());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(dishById.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCart.setName(dishById.getName());
                shoppingCartMapper.insert(shoppingCart);
            }else {
                //套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setNumber(1);
                shoppingCart.setImage(setmeal.getImage());
                shoppingCartMapper.insert(shoppingCart);
        }
    }else {
            aNew.setNumber(aNew.getNumber()+1);
            shoppingCartMapper.updateNumber(aNew);

        }
}

    @Override
    public List<ShoppingCart> showShoppingCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        ShoppingCart shoppingCart1 = shoppingCartMapper.shopByShop(shoppingCart);
        int number = shoppingCart1.getNumber();
        if (number==1){
            shoppingCartMapper.deleteById(shoppingCart1.getId());
        }else {
            shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
            shoppingCartMapper.updateNumber(shoppingCart1);
        }
    }

    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }
}